<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------
/**
 * Common function
 * @author xinqiyang
 * @date   2010-5-3
 *
 */
Load('extend');

/**
 * 返回UserID
 */
function userID()
{
	$uinfo = Session::get('uinfo');
	if(is_array($uinfo))
	return $uinfo['id'];
	return 0;
}

/**
 * 返回用户信息
 * @param $map 查询条件
 */
function userInfo($map)
{
	$u = D('user');
	$user = $u->field('id,enname,image_id,havemail,locationid,city,microblog,score,status')->where($map)->find();
	if(is_array($user))
	{
		return $user;
	}
	return $u->getError();
}

function setUserinfo($user)
{
	Session::set('uinfo',$user);
}

function setSex($sex)
{
	$boy = '';
	$girl = '';
	switch($sex)
	{
		case '1':
			$boy = 'checked disabled';
			$girl = 'disabled';
			break;
		case '2':
			$girl = 'checked disabled';
			$boy = 'disabled';
			break;

	}
	return "<input type=\"radio\" name=\"sex\" value=\"1\" $boy />男 <input type=\"radio\" name=\"sex\" value=\"2\" $girl />女";
}

/**
 * 返回未被删除的图片
 * @param unknown_type $image_id 图片ID
 * @param unknown_type $thum   是否为缩略
 */
function getImage($image_id,$icon='',$thumb=true)
{
	if(!$image_id)
	return C('IMG')."/default/$icon"."user.gif";
	$d=D('image');
	//TODO:  2为删除标记
	//TODO:缓存 这里可以进行内存缓存
	$image = $d->where('`id`='.$image_id.' and `status` != 2 ')->find();
	$label = $thumb ? '/spic/' : '/mpic/';
	$url = is_array($image) ? ($image['url'].'/'.$image['model'].$label.$icon.$image['filename']) : C('IMG')."/default/$icon"."user.gif";
	return $url;

}

/**
 * 是否有新信息
 * @param $lable
 */
function haveMail()
{
	//dump(D('mail')->where('status=1 and touid='.UserID())->select());exit;
	$count = count(D('mail')->where('status=1 and touid='.UserID())->select());
	//dump($count);
	if($count)
		return '<span style="color:#000;">'.$count."条新</span>";
}

/**
 * 获取用户ID
 * @param $uid
 */
function getUser($uid)
{
	$d = D('user');
	//TODO:这里添加头像ID
	$name = $d->field('id,enname,image_id')->find($uid);

	return '<a href="'.__APP__.'/people/space/id/'.$name['id'].'">'.$name['enname'].'</a>';
}

function getUserMicroblog($uid)
{
	$d = D('user');
	//TODO:这里添加头像ID
	$name = $d->field('id,enname,microblog')->find($uid);
	return $name['microblog'];
	//return '<a href="'.__APP__.'/people/space/id/'.$name['id'].'">'.$name['enname'].'</a>';
}

function getUserImg($uid)
{
	return getImage(D('user')->getField('image_id',"id=$uid"),'m_');
}

function getGroupname($id)
{
	return D('group')->getField('title',"id=".$id);
}

/**
 * 图片设置为删除
 * @param unknown_type $image_id
 */
function delImage($image)
{
	$d = D('image');
	//删除单个
	if(is_int($image))
	{
		$d->setField('status','2','`id`='.$image);
		//删除数组
	}elseif(is_array($image)){
		foreach($image as $value)
		{
			$d->setField('status','2','`id`='.$value);
		}
	}else{
		//
	}

}


function getSiteurls($id)
{
	$typea = '';
	$typeb = '';
	$typec = '';
	switch($id)
	{
		case '3':
			$typec = 'checked';
			break;
		case '1':
			$typea = 'checked';
			break;
		case '2':
			$typeb = 'checked';
			break;
		case '4':
			$typed = 'checked';
			break;
		default:
			$typea = 'checked';
			
	}

	return "<input name='category_id' type='radio' value='1' $typea  />购物  <input name='category_id' type='radio' value='2' $typeb />生活  <input name='category_id' type='radio' value='3' $typec />团购  <input name='category_id' type='radio' value='4' $typed />其他";
}

function albumState($id)
{
	$typea = '';
	$typeb = '';
	$typec = '';
	switch($id)
	{
		case '1':
			$typea = 'checked';
			break;
		case '5':
			$typeb = 'checked';
			break;
		case '6':
			$typec = 'checked';
			break;
		default:
			$typea = 'checked';
			break;
	}

	return "<input name='status' type='radio' value='4' $typea  />公开(大家都可见)  <input name='status' type='radio' value='5' $typeb />仅朋友可以看到  <input name='status' type='radio' value='6' $typec />仅自己可以看到";
}

function groupStatus($id)
{
	$typea = '';
	$typeb = '';
	$typec = '';
	switch($id)
	{
		case '4':
			$typea = 'checked';
			break;
		case '5':
			$typeb = 'checked';
			break;
		case '6':
			$typec = 'checked';
			break;
	}

	return "<input name='status' type='radio' value='4' $typea  />公开  <input name='status' type='radio' value='5' $typeb />需要验证  <input name='status' type='radio' value='6' $typec />私有小组(信息仅成员可见)";
}

function getShowType($id)
{
	$typea = '';
	$typeb = '';
	switch($id)
	{
		case '1':
			$typea = 'checked';
			break;
		case '2':
			$typeb = 'checked';
			break;
		default:
			$typea = 'checked';

	}
	return "<input name='showtype' type='radio' value='1' $typea  />推荐大家一起买  <input name='showtype' type='radio' value='2' $typeb />晒晒我买到的";
}

/**
 * 返回查询in ID 条件
 * @param unknown_type $array
 */
function strin($array,$idfield='id')
{
	if(is_array($array))
	{
		$str = "(";
		foreach($array as $value)
		{
			$str .= "'".$value[$idfield]."',";
		}
		return substr($str,0,-1).')';
	}else{
		//返回单条
		return "('$array')";
	}
}


/**
 * 构造单个数组,用于获取实体
 * @param $id
 */
function arrayid($id)
{
	$arr[0] = array('id'=>$id);
	return $arr;
}


/**
 * 匹配URL函数
 * @param unknown_type $url
 */
function getHost($url)
{
	$result = parse_url($url);
	return $result['host'];
}

/**
 * 验证网址的函数
 * @param $domain 网址http://
 */
function checkDomain($domain)
{
	$domain = getHost($domain);
	$d = D('url');
	return $d->where("domain='$domain'")->find();
}
/**
 * TAG处理
 * @param unknown_type $tags
 */
function tagDeal($tags,$modelb,$modelb_id)
{
	if(in_array($modelb,array('url','goods','brand','topic')))
	{
		//替换中文逗号为英文逗号
		$tags = str_replace('，',',',$tags);
		//切割存入数组
		$tagarr = preg_split('/[\s,;]+/',$tags);
		//判断是否存在,存在则返回ID,否则则写入关联表
		$d = D('tags');
		$e = D('modelandmodel');
		$data['modela'] ='tags';

		$data['modelb'] = $modelb;
		$data['modelb_id'] = $modelb_id;
		$data['createtime'] = time();
		$data['status'] = 1;
		$data['user_id'] = userID();
		$data['sequence'] = 20;
		//dump($tagarr);exit;
		foreach($tagarr as $value)
		{
			//判断不为空格
			if($value){
				$id = $d->getField('id',"tag = '$value'");
				//dump($d->getLastSql());
				if($id)
				{
					//写入关联表
					$data['modela_id'] = $id;
					//如果不存在这个关联,则添加
					if(!$e->getField('id',"modela='tags' and modelb = '$modelb' and modelb_id='$modelb_id' and modela_id='".$data['modela_id']."'"))
					{
						$e->add($data);
					}
						
				}else{
					$tag['tag'] = $value;
					$data['modela_id'] = $d->add($tag);
					$e->add($data);
				}
			}

		}
	}
}


function getTags($tags)
{
	$tags = str_replace('，',',',$tags);
	//切割存入数组
	$tagarr = preg_split('/[\s,;]+/',$tags);
	//if()
	$str = '';
	foreach($tagarr as $value){
		$str.='<a class="ml5" href="'.__APP__.'/search/tags/tag/'.$value.'" target="_blank">'.$value.'</a>';
	}
	return $str;
}



	/**
	 * 发送邮件方法
	 * @param unknown_type $mail
	 * @param unknown_type $code
	 */
	 function wsmjsendmail($arr)
	 {
		 
		  import('ORG.Util.SMTP');
		  $smtpserver = "smtp.qq.com";        //SMTP邮件服务器
		  $smtpserverport = 25;    //SMTP端口号 
		  $smtpusermail = "admin@woshimaijia.com";     //SMTP发送邮件地址
		  //这里修改成你自己的帐号和密码
		  $smtpuser = "xxxxxxx";                  //SMTP邮件用户
		  $smtppass = "*******";                  //SMTP邮件密码
		  /**
		   * 进行群发
		   */
		  if(is_array($arr['mail']))
		  {
		  	foreach($arr['mail'] as $value)
		  	{
		  		  $smtpemailto = $value['mail'];          //目标邮件地址 发送给谁
				  $mailsubject = iconv('utf-8','gb2312',$arr['title']);           //邮件主题
				  $mailbody = iconv('utf-8','gb2312',$arr['body']);
				  $mailtype = "HTML";                      //邮件发送方式(HTML/TXT)
				  $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);  //这里面的一个true是表示使用身份验证,否则不使用身份验证.
				  $smtp->debug = true;                     //是否显示发送的调试信息
				  $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
		  		
		  	}
		  	
		  }else{
			   $smtpemailto = iconv('utf-8','gb2312',$arr['mail']);          //目标邮件地址 发送给谁
			  $mailsubject =  iconv('utf-8','gb2312',$arr['title']);        //邮件主题
			  $mailbody = iconv('utf-8','gb2312', $arr['body']); 
			  $mailtype = "HTML";                      //邮件发送方式(HTML/TXT)
			  $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);  //这里面的一个true是表示使用身份验证,否则不使用身份验证.
			  //$smtp->debug = true;             //是否显示发送的调试信息
			  $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
		  }
	}
	

	/**
	 * 返回图片路径
	 * @param $array 包含图片的数组
	 */
	function returnImagePath($array=array(),$size='m_')
	{
		if(is_array($array))
		{
			return $array['url'].'/'.$array['model'].'/spic/'.$size.$array['filename'];
		}
	}
	
	/**
	 * 输出关系
	 */
	function relFriends($id)
	{
		dump($id);
		$array = D('userandmodel')->field("model_id")->where("user_id=".UserID())->select();
		//dump($array);
		if(in_array($id,$array))
		{
			dump('dump friend');
		}elseif($id == UserID()){
			dump('null');
		}else{
			dump('add');
		}
	}

?>
